package com.macro.mall.portal.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.macro.mall.mapper.*;
import com.macro.mall.model.*;
import com.macro.mall.portal.service.SmsFlashPromotionProductRelationService;
import com.macro.mall.portal.service.UmsMemberService;
import com.macro.mall.portal.vo.SmsFlashPromotionProductRelationVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;

@Service
public class SmsFlashPromotionProductRelationServiceImpl implements SmsFlashPromotionProductRelationService {
    @Autowired
    private SmsFlashPromotionProductRelationMapper relationMapper;
    @Autowired
    private PmsProductMapper pmsProductMapper;
    @Autowired
    private PmsSkuStockMapper skuStockMapper;
    @Autowired
    private SmsFlashPromotionSessionMapper flashPromotionSessionMapper;
    @Autowired
    private SmsFlashPromotionProductMemberRelationMapper flashPromotionProductMemberRelationMapper;
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private SmsFlashPromotionMapper flashPromotionMapper;
    @Override
    public SmsFlashPromotionProductRelationVO getPromotionProduct(Long id) {
        SmsFlashPromotionProductRelation smsFlashPromotionProductRelation = relationMapper.selectByPrimaryKey(id);
        SmsFlashPromotionProductRelationVO smsFlashPromotionProductRelationVO = new SmsFlashPromotionProductRelationVO();
        BeanUtils.copyProperties(smsFlashPromotionProductRelation,smsFlashPromotionProductRelationVO);
        smsFlashPromotionProductRelationVO.setPmsProduct(pmsProductMapper.selectByPrimaryKey(smsFlashPromotionProductRelationVO.getProductId()));
        smsFlashPromotionProductRelationVO.setSkuStock(skuStockMapper.selectByPrimaryKey(smsFlashPromotionProductRelationVO.getProductSkuStockId()));
        smsFlashPromotionProductRelationVO.setSmsFlashPromotionSession(flashPromotionSessionMapper.selectByPrimaryKey(smsFlashPromotionProductRelationVO.getFlashPromotionSessionId()));
        UmsMember currentMember = memberService.getCurrentMember();
        if(currentMember!=null){
            SmsFlashPromotionProductMemberRelationExample example = new SmsFlashPromotionProductMemberRelationExample();
            example.createCriteria().andMemberIdEqualTo(currentMember.getId()).andPromotionProductRelationIdEqualTo(smsFlashPromotionProductRelationVO.getId());
            List<SmsFlashPromotionProductMemberRelation> smsFlashPromotionProductMemberRelations = flashPromotionProductMemberRelationMapper.selectByExample(example);
            if (CollUtil.isNotEmpty(smsFlashPromotionProductMemberRelations)){
                smsFlashPromotionProductRelationVO.setSmsFlashPromotionProductMemberRelation(smsFlashPromotionProductMemberRelations.get(0));
            }
        }
        return smsFlashPromotionProductRelationVO;
    }


}